//
//  BookInboxCell.swift
//  WalkNovel
//
//  Created by Kevin on 2023/8/31.
//

import UIKit

class BookInboxCell: UITableViewCell {
    
    lazy var timeBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(.init(named: "black_222222"), for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 12)
        return btn
    }()
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = .init(named: "white_F7F7F7")
        view.wn_cornerWithBorder(8)
        return view
    }()

    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = .init(named: "black_333333")
        label.font = .wn_font(name: .bold, size: 15)
        label.numberOfLines = 0
        return label
    }()
    
    lazy var bookImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "placeholder_cover_ver"))
        image.contentMode = .scaleAspectFill
        image.wn_cornerWithBorder(2)
        image.clipsToBounds = true
        return image
    }()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textColor = .black
        label.numberOfLines = 0
        return label
    }()
    
    lazy var desLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "gray_999999")
        label.numberOfLines = 2
        return label
    }()
    
    var model: MessageModel? {
        didSet {
            if let m = model {
                timeBtn.setTitle((m.ctime)?.wn_toJavaDetailDate(), for: .normal)
                titleLabel.text = m.title
                nameLabel.text = m.name
                desLabel.text = m.des
                bookImage.wn_setupImg(m.coverUrl, placeHolder: "placeholder_cover_ver")
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.addSubview(timeBtn)
        contentView.addSubview(bgView)
        bgView.addSubview(titleLabel)
        bgView.addSubview(bookImage)
        bgView.addSubview(nameLabel)
        bgView.addSubview(desLabel)
        timeBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(12)
        }
        bgView.snp.makeConstraints { (make) in
            make.top.equalTo(timeBtn.snp.bottom).offset(12)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.bottom.equalTo(0)
        }
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(12)
            make.left.equalTo(20)
            make.right.equalTo(-20)
        }
        bookImage.snp.makeConstraints { (make) in
            make.bottom.equalTo(-20)
            make.left.equalTo(20)
            make.size.equalTo(CGSize(width: scaleWidth(61), height: scaleWidth(90)))
        }
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(bookImage.snp.right).offset(8)
            make.top.equalTo(bookImage.snp.top).offset(0)
            make.right.equalTo(-20)
        }
        desLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.left)
            make.top.equalTo(nameLabel.snp.bottom).offset(12)
            make.right.equalTo(-20)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
